app.controller('addHouseController', function ($scope, $controller,$location,addHouseService, uploadService) {

    $controller('baseController', {$scope: $scope});//继承baseController

    //房屋状态
    $scope.sta = ["未出售","已出售"]
    $scope.houseEntity = {images: []}

    /**
     * 修改/添加
     */
    $scope.save = function () {
        var serviceObject;//服务层对象
        if ($scope.houseEntity.id == null) {
            serviceObject = addHouseService.save($scope.houseEntity);
        } else {
            serviceObject =   addHouseService.update($scope.houseEntity);
        }
        // 再添加之前，获得富文本编辑器中的内容。
        $scope.houseEntity.remarks = editor.html();
        //将图片地址转换成JSON字符串
        $scope.houseEntity.images = angular.toJson($scope.houseEntity.images)
        serviceObject.success(
            function (response) {
                if (response.flag) {
                    alert("添加成功");
                    location.href = "house.html";
                } else {
                    alert(response.msg);
                }
            }
        )
    }

    /**
     * 修改数据回显
     */

    $scope.findOne = function () {
        //http://localhost:8111/hosue/admin/addhouse.html#?id=33 问好前面必须加一个#
        var id = $location.search()['id'];
        if(null != id)
        {
            addHouseService.findOne(id).success(
                function (response) {
                    if(response.flag)
                    {
                        console.log(response)
                        $scope.houseEntity = response.data
                        $scope.houseEntity.images = JSON.parse(response.data.images)
                        editor.html($scope.houseEntity.remarks);

                    }else
                    {
                        alert(response.msg)
                    }
                })
        }

    }


    //图片上传
    $scope.imgurl = {}
    $scope.uploadFile = function () {
        uploadService.uploadFile().success(
            function (response) {
                $scope.imgurl.url = response
            }
        )
    }

    //增加照片
    $scope.addImg = function () {
        $scope.houseEntity.images.push($scope.imgurl)
        $scope.imgurl = {}
    }

    //照片删除
    $scope.delImg = function (index) {
        $scope.houseEntity.images.splice(index,1);
    }
})